﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ShopzillaV1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Error(object sender, EventArgs e)
        {
            //Exception exception = Server.GetLastError();
            //Response.Clear();


            //string action;


            //HttpException httpException = exception as HttpException;

            //if (httpException != null)
            //{
            //    switch (httpException.GetHttpCode())
            //    {
            //        case 404:
            //            // page not found
            //            action = "Error404";
            //            break;
            //        case 500:
            //            // server error
            //            action = "Error500";
            //            break;
            //        default:
            //            action = "General";
            //            break;
            //    }
            //}
            //else
            //{
            //    action = "General";
            //}


            //// clear error on server
            //Server.ClearError();

            //Response.Redirect(String.Format("~/Error/{0}", action));

        }


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");





            routes.MapRoute(
                "Home", // Route name
                "", // URL with parameters
                new { controller = "Home", action = "Index" } // Parameter defaults
                );

            routes.MapRoute(
             "Cart", // Route name
             "Cart/{action}/{ASIN}/{ReturnUrl}", // URL with parameters
             new { controller = "Cart", action = "List", id = "All-all", page = 1 } // Parameter defaults
             );


            routes.MapRoute(
                "Product", // Route name
                "{controller}/{action}/{id}/{page}", // URL with parameters
                new { controller = "Product", action = "List", id = "All-all", page = 1 } // Parameter defaults
                );






        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }


        void Application_BeginRequest(object sender, EventArgs e)
        {
            if (!HttpContext.Current.Request.Url.ToString().ToLower().StartsWith("http://localhost"))
            {
                if (!HttpContext.Current.Request.Url.ToString().ToLower().StartsWith("http://www."))
                {
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Status = "301 Moved Permanently";
                    HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().Replace("http://", "http://www."));
                    Response.End();
                }
            }

            //HttpContext context = HttpContext.Current;
            //HttpRequest request = context.Request;
            //if (request.Url.ToString().ToLower().Contains("item.aspx?asin="))
            //{
            //    string newPath = context.Request.Url.ToString().Replace("item.aspx?ASIN=", "Product/ShowProductDetails/");

            //    HttpContext.Current.Response.Clear();
            //    //
            //    HttpContext.Current.Response.Status = "301 Moved Permanently";
            //    string currentUrl = Request.Url.AbsoluteUri;

            //    HttpContext.Current.Response.AddHeader("Location", newPath);
            //    Response.End();
            //}
        }
        //redirect to feed            

    }
}